<?php

declare(strict_types=1);

namespace App\CustHub\Mapper;

use App\CustHub\Model\ChSmartTagRuleConfig;
use Hyperf\Cache\Annotation\Cacheable;
use Hyperf\Config\Annotation\Value;
use Mine\Abstracts\AbstractMapper;

class ChSmartTagRuleConfigMapper extends AbstractMapper
{
    #[Value('cache.default.prefix')]
    protected string $prefix;

    public function assignModel()
    {
        $this->model = ChSmartTagRuleConfig::class;
    }

    #[Cacheable(prefix: 'smart_tag_rule_config',value: '#{uid}_#{tagGroupId}',ttl: 15 * 60)]
    public function findByTagGroupId(mixed $uid,mixed $tagGroupId)
    {
        return $this->model::query()->where('uid',$uid)
            ->where('tag_group_id',$tagGroupId)
            ->first();
    }

    public function clearCache(mixed $uid,mixed $tagGroupId): void
    {
        $redis = redis();
        $keyOne = $this->prefix . 'smart_tag_rule_config:'.$uid.'_'.$tagGroupId;
        $redis->del([$keyOne]);
    }
}